package com.jeeplus.test.jlnu.deanOffice.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jeeplus.test.jlnu.admissionInfo.domain.entity.AdmissionInfo;
import com.jeeplus.test.jlnu.admissionInfo.domain.entity.Supplement;
import com.jeeplus.test.jlnu.admissionInfo.mapper.ExcelMapper;
import com.jeeplus.test.jlnu.deanOffice.domain.entity.JlnuDeansOffice;
import com.jeeplus.test.jlnu.deanOffice.domain.vo.JlnuDeansOfficeDto;
import com.jeeplus.test.jlnu.deanOffice.mapper.DeansOfficeMapper;
import com.jeeplus.test.jlnu.deanOffice.service.DeansOfficeService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Field;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class DeansOfficeServiceImpl implements DeansOfficeService {

    @Autowired
    private DeansOfficeMapper deansOfficeMapper;

    @Autowired
    private ExcelMapper excelMapper;

    @Override
    public Page<Map<String, Object>> getAdmissionInfoPage(JlnuDeansOfficeDto jlnuDeansOfficeDto, Page<JlnuDeansOfficeDto> page, String officeId) {

        if (StringUtils.isNotEmpty(jlnuDeansOfficeDto.getApproveState())) {
            String[] arr = jlnuDeansOfficeDto.getApproveState().split(",");
            List<String> list = Arrays.asList(arr);
            jlnuDeansOfficeDto.setApproveStateList(list);
        }

        Page<JlnuDeansOfficeDto> jlnuDeansOfficePage=deansOfficeMapper.selectAdmissionInfoByOffice(jlnuDeansOfficeDto,officeId,page);
        List<JlnuDeansOfficeDto> records = jlnuDeansOfficePage.getRecords();
        List<Map<String, Object>> jlnuDeansOfficeRecords = new ArrayList<>();
        // 2. 遍历每条记录
        for (JlnuDeansOfficeDto record : records) {
            Map<String, Object> recordMap = new HashMap<>();

            // 3. 将 AdmissionInfoRespVo 中的字段放入 map
            Field[] fields = JlnuDeansOfficeDto.class.getDeclaredFields();
            for (Field field : fields) {
                field.setAccessible(true);
                try {
                    // 获取字段值并添加到 map 中
                    Object value = field.get(record);

                    if(field.getName().equals("lqxy")){
                        recordMap.put("学院", value);
                    }else if(field.getName().equals("zymc")){
                        recordMap.put("专业名称", value);
                    }
                    else if(field.getName().equals("wbd")){
                        recordMap.put("未报到", value);
                    }else{
                        recordMap.put(field.getName(), value);
                    }

                    recordMap.put(field.getName(), value);
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }

            // 5. 添加处理后的记录
            jlnuDeansOfficeRecords.add(recordMap);
        }
        Page<Map<String, Object>> modifiedPage = new Page<>(jlnuDeansOfficePage.getCurrent(), jlnuDeansOfficePage.getSize(), jlnuDeansOfficePage.getTotal());
        modifiedPage.setRecords(jlnuDeansOfficeRecords);
        return modifiedPage;
    }
    @Override
    public void addJlnuDeansOffice(List<JlnuDeansOffice> jlnuDeansOffices) {
        List<AdmissionInfo> admissionInfos = jlnuDeansOffices.stream()
                .map(StudentInfoEntity -> {
                    AdmissionInfo admissionInfo = new AdmissionInfo();
                    //admissionInfo.setExamineeNum(StudentInfoEntity.getKsh());
                    admissionInfo.setOfficeId(StudentInfoEntity.getOfficeId());
                    //admissionInfo.setAdmissionNum(StudentInfoEntity.getExamNumber());
//                    admissionInfo.setStudentId(StudentInfoEntity.getZxshxh());
//                    admissionInfo.setName(StudentInfoEntity.getName());
//                    admissionInfo.setGender(StudentInfoEntity.getGender());
//                    admissionInfo.setBirthday(StudentInfoEntity.getBirthDate());
//                    admissionInfo.setPoliticalOutlook(StudentInfoEntity.getPoliticalStatus());
//                    admissionInfo.setNation(StudentInfoEntity.getEthnicity());
//                    admissionInfo.setIdCard(StudentInfoEntity.getIdNumber());
//                    admissionInfo.setPlanNature(StudentInfoEntity.getSpecialProgram());
//                    // 设置录取学校为吉林师范大学
//                    admissionInfo.setAdmissionSchools("吉林师范大学");
//                    admissionInfo.setAdmissionAcademy(StudentInfoEntity.getCollege());
//                    admissionInfo.setAdmissionMajor(StudentInfoEntity.getMajorName());
//                    //admissionInfo.setAdmissionTime(StudentInfoEntity.getNf()); //年份没有
//                    //admissionInfo.setGradation(StudentInfoEntity.getZycc()); //专业层次没有
//                    //admissionInfo.setEducationalSystem(StudentInfoEntity.getZyxz());//专业性质没有
//                    // TODO: 学习形式字段暂时没有
//                    // admissionInfo.setLearningForms(StudentInfoEntity.get);
//                    admissionInfo.setExamineeSort(StudentInfoEntity.getStudentCategory());
//                    //admissionInfo.setBatch(StudentInfoEntity.getPcmc());//批次没有
//                    admissionInfo.setGraduationSchool(StudentInfoEntity.getPreviousSchoolName());
//                    admissionInfo.setSourceArea(StudentInfoEntity.getCandidateSource());
//                    admissionInfo.setAreaName(StudentInfoEntity.getCandidateSource());
//                    admissionInfo.setHomeAddress(StudentInfoEntity.getResidenceAddress()); //这里用的户口地址
//                    admissionInfo.setPostalCode(StudentInfoEntity.getMailingZipCode());
//                    admissionInfo.setPhoneNum(StudentInfoEntity.getContactPhoneNumber());
//                    admissionInfo.setInfoSource("研究生院招生数据");
//                        admissionInfo.setFlag("3");
                    // TODO: 信息源和备注字段暂时没有
                    // admissionInfo.setInfoSource();
                    // admissionInfo.setRemarks();

                    return admissionInfo; // 返回生成的 AdmissionInfo 对象
                }).collect(Collectors.toList());
        List<JlnuDeansOffice> jlnuDeansOffice = jlnuDeansOffices.stream()
                .map(e -> {
                    //PostgraduateReqVo admissionInfo = new AdmissionInfo();
                    e.setStudentid(e.getXh());

                    return e; // 返回生成的 AdmissionInfo 对象
                }).collect(Collectors.toList());// 收集到 admissionInfos 列表中
        deansOfficeMapper.saveDeansOffice(jlnuDeansOffice);
        // 插入录取信息表全部数据
        excelMapper.saveFirstData(admissionInfos);
        // 插入学籍信息表（只插入学号）
        excelMapper.saveSecondData(admissionInfos);
        // 插入毕业信息表（只插入学号）
//        excelMapper.saveThirdData(admissionInfos);
        // 插入就业信息表（只插入学号）
        excelMapper.saveFourth(admissionInfos);
        // 插入学位信息表（只插入学号）
        excelMapper.saveFifth(admissionInfos);
        // 插入学历信息表（只插入学号）
        excelMapper.saveSixth(admissionInfos);
        // 插入转递（邮寄信息表）
        excelMapper.saveSeventh(admissionInfos);
        // 档案信息表
//        excelMapper.saveEighth(admissionInfos);
        //往学生端插入学号数据
        // 创建两个列表来存储不同 flag 的对象
        excelMapper.saveFourteenth(admissionInfos);
        excelMapper.saveFifteenth(admissionInfos);
        List<AdmissionInfo> flagOneList = new ArrayList<>();
        List<AdmissionInfo> otherFlagList = new ArrayList<>();

        for (AdmissionInfo admissionInfo : admissionInfos) {
            if ("1".equals(admissionInfo.getFlag())) {
                flagOneList.add(admissionInfo);
            } else {
                otherFlagList.add(admissionInfo);
            }
        }

        // 对于 flag 为 1 的对象调用保存方法
        if (!flagOneList.isEmpty()) {
            excelMapper.saveNinth(flagOneList);
            excelMapper.saveTenth(flagOneList);
            excelMapper.saveEleth(flagOneList);
            excelMapper.saveThirteenth(flagOneList);
        }

        // 对于 flag 为 2 或其他的对象调用保存方法
        if (!otherFlagList.isEmpty()) {
            excelMapper.saveTwelfth(otherFlagList);
            excelMapper.saveSixteenth(otherFlagList);
            excelMapper.saveSeventeenth(otherFlagList);
            excelMapper.saveEighteenth(otherFlagList);
        }

    }

    @Override
    public void update(JlnuDeansOfficeDto jlnuDeansOfficeDto) {
        deansOfficeMapper.updateDeanOffice(jlnuDeansOfficeDto);


    }

    @Override
    public JlnuDeansOfficeDto selectDeanOfficeById(String stuId) {
        JlnuDeansOfficeDto jlnuDeansOffice = deansOfficeMapper.getDeanOfficeInfoById(stuId);
        return jlnuDeansOffice;
    }
    public static Map<String, Object> convertToMap(Object dto) throws IllegalAccessException {
        Map<String, Object> map = new HashMap<>();
        Class<?> clazz = dto.getClass();
        for (Field field : clazz.getDeclaredFields()) {
            field.setAccessible(true);
            map.put(field.getName(), field.get(dto));
        }
        return map;
    }


}
